package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-16 17:09
 **/
@RestController
@RequestMapping(value = "/article")
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 添加到elasticsearch
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Article article){
        articleSearchService.add(article);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    @GetMapping(value = "/{keyword}/{page}/{size}")
    public Result findByKeyword(@PathVariable String keyword, @PathVariable int page, @PathVariable int size){
        Page<Article> pageData =  articleSearchService.findByKeyword(keyword, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(pageData.getTotalElements(), pageData.getContent()));
    }
}
